<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Services\Logger;
use App\Services\Store;

class Mqtt extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mqtt {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'mqtt command';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $action = $this->argument('action');
        switch ($action) {
            case 'start':
                $this->start();
                break;
            case 'stop':
                $this->stop();
                break;
            case 'restart':
                $this->restart();
                break;
        }
    }

    private function start()
    {
        $host = '123.207.28.142';
        $port = 1883;

        $r = new \mqttclient\src\swoole\MqttClient($host, $port, 10016);
        $r->setKeepAlive(60);
        $r->setLogger(new Logger());
        $r->setStore(new Store());
        $r->setTopics(
            [
                new \mqttclient\src\subscribe\Topic('topicTest03', function ($msg) {
                    echo "I receive:" . $msg . "\r\n";
                })
            ]
        );

        $r->on(\mqttclient\src\consts\ClientTriggers::RECEIVE_SUBACK, function (\mqttclient\src\swoole\MqttClient $client) {
            echo "publish success". "\r\n";
        });

        $r->on(\mqttclient\src\consts\ClientTriggers::RECEIVE_CONNACK, function (\mqttclient\src\swoole\MqttClient $client) {
            echo "recieve success". "\r\n";
//            $client->publish('topicTest03','GGXX',\mqttclient\src\consts\Qos::ONE_TIME);

        });

        $r->connect();

    }

    private function stop()
    {
        $host = '123.207.28.142';
        $port = 1883;

        $r = new \mqttclient\src\swoole\MqttClient($host,$port,10017);
        $r->setAuth('username','password');
        $r->setKeepAlive(60);
        $r->setLogger(new Logger());
        $r->setStore(new Store());
        $r->setTopics(
            [
                new \mqttclient\src\subscribe\Topic('test/slim',function($msg){
                    echo "I receive:".$msg."\r\n";}),
                new \mqttclient\src\subscribe\Topic('test/slim3',function(\mqttclient\src\swoole\MqttClient $client,$msg){
                    echo "I receive:".$msg." for slim3 \r\n";
                    echo $client->getClientId();
                })
            ]
        );

//set trigger
        $r->on(\mqttclient\src\consts\ClientTriggers::RECEIVE_SUBACK,function(\mqttclient\src\swoole\MqttClient $client){
            $client->publish('slim/echo','GGXX',\mqttclient\src\consts\Qos::ONE_TIME);
        });

        $r->connect();
        $r->publish('test/slim','test qos',2);
    }

    private function restart()
    {

    }
}
